/*
 * Socket.h
 *
 *  Created on: 2012-12-7
 *      Author: yangentao@gmail.com
 */

#pragma once;
#include <WinSock2.h>
#include <windows.h>
#include <string>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include "NetUtil.h"
#include "SocketOption.h"
#include "../String.h"
#include "../Thread.h"

using namespace std;


namespace yet {


class Socket {
public:
	Socket();
	Socket(SOCKET s);
	virtual ~Socket();
public:
	bool createTcpSocket();
	bool createUdpSocket();
	void close();
	bool isValidSocket();

	// throw (SocketError);
	bool bind();
	bool bind(short localPort);
	bool bind(string localIP, short localPort);
	bool bind(InetAddr addr);

	//SOMAXCONN, 0x7fffffff
	void listen();
	void listen(int backlog);

	//throw (SocketError)
	//after bind!
	InetAddr getSockName();
	InetAddr getPeerName();

	//throw (SocketError)
	bool connect(InetAddr peerAddr);
	bool connect(string peerIp, short peerPort);

	//throw (SocketError)
	int send(const char* buffer, int size, int flag);
	//throw (SocketError)
	int recv(char* buffer, int size, int flag);

	int send(const char* buffer, int size);
	int recv(char* buffer, int size);

	int sendOOB(const char* buffer, int size);
	int recvOOB(char* buffer, int size);

	int sendTo(InetAddr peerAddr, const char* buf, int len);
	int recvFrom(InetAddr* outPeerAddr, char* buf, int len);
	SOCKET accept();
	SOCKET accept(InetAddr* outAddr);
	//after read
	bool hasMoreData();

	bool selectRead();
	bool selectWrit();
	bool selectExcept();

	bool select(FdSet* readSet, FdSet* writeSet, FdSet* exceptionSet, int seconds, int usec = 0);

	void ioctl(long cmd, u_long* arg);
	void ioctlNoBlock(BOOL noBlock);
	u_long ioctlRead();

//	int eventSelect();

public:
	SOCKET sock;
};




} /* namespace yet */
